package Test;

/**
 * @leiwenkang 2017-12-29 14:53.
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] array = {3, 6, 4, 7, 9, 1, 5, 8, 2};
        quickSort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }

    public static void quickSort(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }
        quickSort(array, 0, array.length - 1);
    }

    private static void quickSort(int[] array, int low, int high) {
        if (low >= high) {
            return;
        }
        int pos = partition(array, low, high);
        quickSort(array, low, pos - 1);
        quickSort(array, pos + 1, high);
    }

    private static int partition(int[] array, int low, int high) {
        int value = array[low];
        while (low < high) {
            while (low < high && array[high] >= value) {
                high--;
            }
            array[low] = array[high];
            while (low < high && array[low] <= value) {
                low++;
            }
            array[high] = array[low];
        }
        array[low] = value;
        return low;
    }
}
